﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;



namespace UltrasonicTDPlatform
{
    public partial class DriverForm : Form
    {
        int DriFreq = 20500;  //驱动设置频率
        int Drivol = 100;     //驱动电压
        int Dricur = 5;       //驱动电流
        int Dripow = 1000;    //驱动功率

        /*
         * 用于存放显示数据的队列，以及队列的一些信息
         */
        private Queue<double> dataQueue = new Queue<double>(100);
        private int curValue = 0;
        private int num = 5;//每次删除增加几个点

        public DriverForm()
        {
            InitializeComponent();
            this.TopLevel = false;
            InitChart(chart1);
        }

        /// <summary>
        /// 初始化图表
        /// </summary>
        private void InitChart(Chart chart)
        {

            //定义图表区域
            chart.ChartAreas.Clear();
            ChartArea chartArea1 = new ChartArea("C1");
            chart.ChartAreas.Add(chartArea1);
            chartArea1.BackColor = System.Drawing.Color.Transparent;
            //定义存储和显示点的容器
            chart.Series.Clear();
            Series series1 = new Series("S1");
            series1.ChartArea = "C1";
            chart.Series.Add(series1);
            //设置图表显示样式
            chart.ChartAreas[0].AxisY.Minimum = 0;
            chart.ChartAreas[0].AxisY.Maximum = 100;
            chart.ChartAreas[0].AxisX.Interval = 5;
            chart.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
            chart.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
            //设置标题
            chart.Titles.Clear();
            chart.Titles.Add("S01");
            chart.Titles[0].Text = "驱动电压显示";
            chart.Titles[0].ForeColor = Color.RoyalBlue;
            chart.Titles[0].Font = new System.Drawing.Font("幼圆", 12F);
            
            //隐藏图例
            // Exclude entries from the legend.
            chart.Legends[0].Enabled = false;
            chart.ForeColor = Color.White;
            
            
            //设置图表显示样式
            double element = 100;
            chart.ChartAreas[0].AxisY.Minimum = 0;
            chart.ChartAreas[0].AxisY.Maximum = element;
            chart.ChartAreas[0].AxisY.Interval = element / 5;

            chart.ChartAreas[0].AxisX.Minimum = 0;
            chart.ChartAreas[0].AxisX.Maximum = 100;
            chart.ChartAreas[0].AxisX.Interval = 20;

            
            chart.ChartAreas[0].AxisX.Title = "时间 / us";
            chart.ChartAreas[0].AxisY.Title = "幅值 / V";
            chart.ChartAreas[0].AxisX.TitleFont = new System.Drawing.Font("幼圆", 12F);
            chart.ChartAreas[0].AxisY.TitleFont = new System.Drawing.Font("幼圆", 12F);

            //图表字体颜色
            chart.ChartAreas[0].AxisY.TitleForeColor = Color.Silver;
            chart.ChartAreas[0].AxisX.TitleForeColor = Color.Silver;
        
            chart.ChartAreas[0].AxisX.LineColor = Color.Silver;
            chart.ChartAreas[0].AxisY.LineColor = Color.Silver;
            chart.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Silver;
            chart.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Silver;
            chart.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Silver;
            chart.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.Silver;
            //chart.ChartAreas[0].AxisY.

            chart1.ChartAreas[0].AxisX.LabelStyle.Format = "F0";//2位小数
            chart1.ChartAreas[0].AxisY.LabelStyle.Format = "F0";//3位小数
            //设置图表显示样式
            chart.Series[0].Color = Color.Red;
            chart.Series[0].ChartType = SeriesChartType.Spline;
            chart.Series[0].Points.Clear();
            chart.Series[0].Points.AddXY(0, 0);
        }

        /// <summary>
        /// 定时器事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            UpdateQueueValue();
            this.chart1.Series[0].Points.Clear();
            for (int i = 0; i < dataQueue.Count; i++)
            {
                this.chart1.Series[0].Points.AddXY((i + 1), dataQueue.ElementAt(i));
            }
        }

        /// <summary>
        /// 更新队列中的值
        /// </summary>
        private void UpdateQueueValue()
        {

            if (dataQueue.Count > 100)
            {
                //先出列
                for (int i = 0; i < num; i++)
                {
                    dataQueue.Dequeue();
                }
            }
            for (int i = 0; i < num; i++)
            {
                //对curValue只取[0,360]之间的值
                curValue = curValue % 360;
                //对得到的正玄值，放大50倍，并上移50
                dataQueue.Enqueue((50 * Math.Sin(curValue * Math.PI / 180)) + 50);
                curValue = curValue + 10;
            }
            
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
        {

        }

        private void DriverForm_Load(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                //相关值装载
                NUDfreq.Value = DriFreq;
                NUDCur.Value = Dricur;
                NUDpow.Value = Dripow;
                NUDvol.Value = Drivol;
            }
                
           
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged_2(object sender, EventArgs e)
        {
            if(RBCV.Checked||RBCP.Checked||RBCA.Checked)
            {
                CBRFT.Checked = false;
            }
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            CBRFT.Checked = false;   
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            CBRFT.Checked = false;
        }

        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
            CBRFT.Checked = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!timer1.Enabled)
            {
                timer1.Start();
                pictureBox1.BackgroundImage = Properties.Resources.jieshu;
                pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
                button1.Text = "    结束";
            }
            else
            {
                timer1.Stop();
                pictureBox1.BackgroundImage = Properties.Resources.qidong;
                pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
                button1.Text = "    启动";
            }
            

        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }

        private void label25_Click(object sender, EventArgs e)
        {

        }

        private void numericUpDown1_ValueChanged_1(object sender, EventArgs e)
        {

        }
    }
}
